//
//  StatusDetailViewController.m
//  Weibo
//
//  Created by Anasue on 10/2/15.
//  Copyright © 2015 vvusu. All rights reserved.
//

#import "StatusDetailViewController.h"
#import "StatusDetailViewFrame.h"
#import "StatusDetailView.h"
#import "StatusDetailToolBar.h"
#import "StatusDetailBottomToolBar.h"
#import "StatusDetailTopToolBar.h"
#import "Status.h"
#import "Comment.h"
#import "StatusTool.h"
#import "User.h"
#import "UIImageView+WebCache.h"

@interface StatusDetailViewController ()<UITableViewDataSource,UITableViewDelegate,StatusDetailTopToolBarDelegate>
@property (nonatomic, weak) UITableView *detailTableView;
@property (nonatomic, strong) StatusDetailTopToolBar *topToolBar;
@property (nonatomic, strong) NSMutableArray *comments;
@property (nonatomic, strong) NSMutableArray *reposts;

@end

@implementation StatusDetailViewController

#pragma mark - 懒加载
- (StatusDetailTopToolBar *)topToolBar
{
    if (_topToolBar == nil) {
        _topToolBar = [StatusDetailTopToolBar toolBar];
        _topToolBar.status = self.status;
        _topToolBar.delegate = self;
    }
    return _topToolBar;
}

- (NSMutableArray *)comments
{
    if (_comments == nil) {
        _comments = [NSMutableArray array];
    }
    return _comments;
}

- (NSMutableArray *)reposts
{
    if (_reposts == nil) {
        _reposts = [NSMutableArray array];
    }
    return _reposts;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"微博正文";
    [self setupTableView];
    [self setupDetailView];
    [self setupBottomToolBar];
}

- (void)setupBottomToolBar
{
    StatusDetailBottomToolBar *bottomToolBar = [[StatusDetailBottomToolBar alloc]init];
    bottomToolBar.width = self.view.width;
    bottomToolBar.height = self.view.height - self.detailTableView.height;
    bottomToolBar.y = CGRectGetMaxY(self.detailTableView.frame);
    [self.view addSubview:bottomToolBar];
}

- (void)setupTableView
{
    UITableView *tableView = [[UITableView alloc]init];
    tableView.backgroundColor = Color(211, 211, 211);
    tableView.width = self.view.width;
    tableView.height = self.view.height - 44;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.delegate = self;
    tableView.dataSource = self;
    self.detailTableView = tableView;
    [self.view addSubview:self.detailTableView];
}

- (void)setupDetailView
{
    StatusDetailView *detailView = [[StatusDetailView alloc]init];
    StatusDetailViewFrame *detailViewFrame = [[StatusDetailViewFrame alloc]init];
    detailViewFrame.status = self.status;
    detailView.detailViewFrame = detailViewFrame;
    detailView.height = detailViewFrame.frame.size.height;
    self.detailTableView.tableHeaderView = detailView;
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.topToolBar.selectedBtnType == StatusDetailTopToolBarCommentBtn) {
        return self.comments.count;
    }else{
        return self.reposts.count;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return self.topToolBar;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return self.topToolBar.height;
}

#pragma mark - UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
//    cell.imageView.image = [UIImage imageWithName:user.profile_image_url];
    if (self.topToolBar.selectedBtnType == StatusDetailTopToolBarCommentBtn) {
        Comment *comment = self.comments[indexPath.row];
        cell.detailTextLabel.text = comment.text;
        
                User *user = comment.user;
                cell.textLabel.text = user.name;
        [cell.imageView setImageWithURL:[NSURL URLWithString:user.profile_image_url] placeholderImage:[UIImage imageWithName:@"avatar_default_small"]];
    }else {
        Status *status = self.reposts[indexPath.row];
        cell.detailTextLabel.text = status.text;
        User *user = status.user;
        cell.textLabel.text = user.name;
        [cell.imageView setImageWithURL:[NSURL URLWithString:user.profile_image_url] placeholderImage:[UIImage imageWithName:@"avatar_default_small"]];
    }
    return cell;
}

#pragma mark - StatusDetailTopToolBarDelegate

- (void)statusDetailTopToolBar:(StatusDetailTopToolBar *)toolBar didSelectedBtn:(StatusDetailTopToolBarBottonType)bottonType
{
    self.topToolBar.selectedBtnType = bottonType;
    switch (bottonType) {
        case StatusDetailTopToolBarCommentBtn:
            [self loadComment];
            break;
        case StatusDetailTopToolBarRetweetedBtn:
            [self loadRetweeted];
            break;
        default:
            break;
    }
}


- (void)loadComment
{
    CommentParam *param = [CommentParam param];
    param.id = self.status.idstr;
    Comment *comment = [self.comments firstObject];
    param.since_id = comment.idstr;
    [StatusTool commentsWithParams:param success:^(CommentResult *result) {
        self.status.comments_count = result.total_number;
        self.topToolBar.status = self.status;
        NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, result.comments.count)];
        [self.comments insertObjects:result.comments atIndexes:set];
        [self.detailTableView reloadData];
    } failure:^(NSError *error) {
        Log(@"%@",error);
    }];
}

- (void)loadRetweeted
{
    RetweetedParam *param = [RetweetedParam param];
    param.id = self.status.idstr;
    Status *status = [self.reposts firstObject];
    param.since_id = status.idstr;
    [StatusTool retweetedWithParams:param success:^(RetweetedResult *result) {
        self.status.reposts_count = result.total_number;
        self.topToolBar.status = self.status;
        NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, result.reposts.count)];
        [self.reposts insertObjects:result.reposts atIndexes:set];
        [self.detailTableView reloadData];
    } failure:^(NSError *error) {
        Log(@"%@",error);
    }];
}


@end
